package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.LaborContractAddDTO;
import com.oa.pm.dto.LaborContractParamDTO;
import com.oa.pm.entity.LaborContract;
import com.oa.pm.entity.LaborContractFile;
import com.oa.pm.mapper.LaborContractMapper;
import com.oa.pm.service.ILaborContractFileService;
import com.oa.pm.service.ILaborContractService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.LaborContractDetailsVO;
import com.oa.pm.vo.LaborContractsListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 劳动合同表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class LaborContractServiceImpl extends ServiceImpl<LaborContractMapper, LaborContract> implements ILaborContractService {

    private final LaborContractMapper laborContractMapper;
    private final ILaborContractFileService laborContractFileService;

    @Autowired
    public LaborContractServiceImpl(LaborContractMapper laborContractMapper,
                                    ILaborContractFileService laborContractFileService) {
        this.laborContractMapper = laborContractMapper;
        this.laborContractFileService = laborContractFileService;
    }

    @Override
    @CommonService
    public PageResult<LaborContract> findList(LaborContractParamDTO laborContractParamDTO) {
        Integer pageNum = laborContractParamDTO.getPageNum();
        Integer pageSize = laborContractParamDTO.getPageSize();

        if (pageNum == null) {
            List<LaborContract> list = laborContractMapper.findList(laborContractParamDTO);
            return new PageResult<>(list);
        } else {
            // 当前页，总条数 构造 page 对象
            Page<LaborContract> page = new Page<>(pageNum, pageSize);
            //查询
            List<LaborContract> list = laborContractMapper.findList(page, laborContractParamDTO);
            return new PageResult<>(list);
        }
    }

    /**
     * 根据id查询
     */
    @Override
    @CommonService
    public LaborContract findLaborContractById(String id) {
        return laborContractMapper.findById(id);
    }

    @Override
    @Transactional
    public com.joneying.common.web.response.Resp add(LaborContractAddDTO laborContractAddDTO) {
        try {
            LaborContract laborContract = new LaborContract();
            BeanUtils.copyProperties(laborContractAddDTO, laborContract, "laborContractFileList");
            laborContract.insert();
            List<LaborContractFile> laborContractFileList = laborContractAddDTO.getLaborContractFileList();
            if (laborContractFileList != null && laborContractFileList.size() > 0) {
                laborContractFileList.forEach(p -> {
                    p.setUploadTime(new Date());
                    p.setLaborContractId(laborContract.getId());
                });
                laborContractFileService.insertBatch(laborContractFileList);
            }

        } catch (BeansException e) {
            e.printStackTrace();
            return new Resp().failure();
        }
        return new Resp().success();
    }

    @Transactional
    @Override
    public Resp edit(LaborContractAddDTO laborContractAddDTO) {
        try {
            LaborContract laborContract = new LaborContract();
            BeanUtils.copyProperties(laborContractAddDTO, laborContract, "laborContractFileList");
            laborContract.updateById();
            List<LaborContractFile> laborContractFileList = laborContractAddDTO.getLaborContractFileList();
            if (laborContractFileList != null && laborContractFileList.size() > 0) {
                laborContractFileService.delete(new EntityWrapper().eq("labor_contract_id", laborContract.getId()));
                laborContractFileList.forEach(p -> {
                    p.setUploadTime(new Date());
                    p.setLaborContractId(laborContract.getId());
                });
                laborContractFileService.insertBatch(laborContractFileList);
            }

        } catch (BeansException e) {
            e.printStackTrace();
            return new Resp().failure();
        }
        return new Resp().success();
    }

    @Transactional
    @Override
    public PageResult<LaborContractsListVO> findLaborContractList(Map condition) {
        // 当前页，总条数 构造 page 对象
        Page<LaborContract> page = new Page<>(Integer.valueOf(condition.get("pageNum").toString()), Integer.valueOf(condition.get("pageSize").toString()));
        List<LaborContractsListVO> laborContractList = laborContractMapper.findLaborContractList(page, condition);
        return new PageResult<>(laborContractList, page);
    }

    @Override
    public LaborContractDetailsVO findLaborContractDetails(String id) {

        return laborContractMapper.findLaborContractDetails(id);
    }

    @Override
    public void export(Map condition, HttpServletResponse response) {
        List<LaborContractsListVO> laborContractList = null;
        if(condition != null && condition.size() > 0){
            Page<LaborContract> page = new Page<>(Integer.valueOf(condition.get("pageNum").toString()), Integer.valueOf(condition.get("pageSize").toString()));
            laborContractList = laborContractMapper.findLaborContractList(page, condition);
        }else{
            laborContractList = laborContractMapper.findLaborContractList(new Page<>(), condition);
        }
        String[] rows = {"序号", "姓名", "劳动合同签约单位", "所属部门", "岗位", "职务", "劳动合同开始时间", "劳动合同结束时间", "劳动合同提醒时间", "试用期结束时间", "已签次数", "合同期种类", "是否续签", "附件下载地址", "备注"};
        try {
            ExcelUtil.downloadExcel("劳动合同列表" + DateUtil.getDayMonth(), rows, laborContractList, "劳动合同列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
